package com.company.basic;
public class BinaryNum {


    public int countOne(int num){
        int bitCount = Integer.SIZE;
        System.out.println("十进制显示 "+num);
        System.out.println("二进制显示 "+Integer.toBinaryString(num));
        System.out.println("二进制的位数 "+bitCount);
        int result = 0;
        int mask = 0x01;
        for (int i = 0; i < bitCount; i++) {
            int moved = num>>>i;
            System.out.println("向右 -》 "+Integer.toBinaryString(moved));
            int masked = moved & mask;
            System.out.println("与 0000 0001 位与后 "+masked);
            if (masked == 1) {
                result++;
            }
        }
        return result;
    }
}
